package com.cordys.uc.webapps.translator;

import java.io.File;
import java.io.IOException;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;

import org.w3c.dom.Document;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.xml.sax.SAXException;

public class XFormTranslator {
	private File srcFile;

	XFormTranslator(File srcFile) {
		this.srcFile = srcFile;
	}

	void translate() throws SAXException, IOException, ParserConfigurationException {
		DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance(); //For getting dom object of caf file
        DocumentBuilder dBuilder = dbFactory.newDocumentBuilder();
        Document doc = dBuilder.parse(srcFile);
        //get the nodelist
        NodeList nodelist = doc.getElementsByTagName("xforms:label"); //list of all nodes having "label" tag
		TranslationLogger.writeCafLabelstoXL(srcFile.getAbsolutePath(), nodelist);
		Node CDATA =  doc.getElementsByTagName("xformsscript").item(0); //get xform embedded script
		if(CDATA!=null)
		{
		String jsContent = CDATA.getTextContent();
		InlineJavascriptTranslator.processinlinejs(jsContent, srcFile.getAbsolutePath()); 
		}
	}
}
